<?php

namespace app\common\crontab;

use app\common\cachemodel\ZlcrCustomReportQuestion;
use app\common\cachemodel\ZlcrCustomReportQuestionComment;
use think\Controller;
use think\Env;
use think\Log;
use XznsTools\cache\CacheManage;

class CustomReport extends Controller
{
    protected $reportNoReceivedTime;//支付后接单时长，俩小时
    protected $completeAnswerTime;//接单后完成时长，六小时
    protected $reportCommentTime;//超时未评论时长，三天

    protected function _initialize()
    {
        parent::_initialize();
        $this->reportNoReceivedTime = Env::get('queue.custom_report_received_delay');
        $this->completeAnswerTime = Env::get('queue.custom_report_answer6_delay');
        $this->reportCommentTime = Env::get('queue.custom_report_comment_delay');
    }

    /**
     * 未接单问题报告失效处理
     * @return bool
     */
    private function noReceivedReport()
    {
        //两小时报告未被接单则处理为失效
        try
        {
            $overTime = time() - $this->reportNoReceivedTime;
            $questionModel = new ZlcrCustomReportQuestion();
            $questionIds = $questionModel->where(['pay_time' => $overTime, 'status' => ['in', [1, 2]]])->column('id');
            $questionModel->where(['id' => ['in', $questionIds]])->update(['status' => 11]);//订单派发超时失效
        }
        catch (\Exception $e)
        {
            Log::write('获取报告接单超时数据失败：' . $e->getMessage(), 'error', true);
            return false;
        }
        return true;
    }


    /**
     * 问题报告解答超时失效处理
     * @return bool
     */
    private function answerOvertime()
    {
        //六小时报告未被回答则处理为失效
        try
        {
            $overTime = time() - $this->completeAnswerTime;
            $questionModel = new ZlcrCustomReportQuestion();
            //3=>问题待解答 4=>解答待审核  13=>问题待解答（答案审核未通过）
            $questionIds = $questionModel->where(['astr_receiving_time' => $overTime, 'status' => ['in', [3, 4, 13]]])->column('id');
            $questionModel->where(['id' => ['in', $questionIds]])->update(['status' => 12]);//解答超时失效
        }
        catch (\Exception $e)
        {
            Log::write('获取报告解答超时数据失败：' . $e->getMessage(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 评论超时，自动评论
     * @return bool
     */
    public function commentOvertime()
    {
        //获取超时未评论的问题
        try
        {
            $questionModel = new ZlcrCustomReportQuestion();
            $overTime = time() - $this->reportCommentTime;
            //5=>已完成待评价 6=>已评价待审核
            $questionInfo = $questionModel->where(['q.answer_time' => $overTime, 'status' => ['in', [5]]])->column('uid,astr_id', 'id');
            $inserData = [];
            $qids = [];
            $uid = 0;
            foreach ($questionInfo as $key => $item)
            {
                $inserData[] = [
                    'qid' => $key,
                    'level' => 3,
                    'tid' => 0,//平淡无味
                    'status' => 2,
                    'comment' => '',
                    'star_id' => $item['astr_id'],
                    'comment_type' => 2,
                    'aid' => 0,
                    'create_time' => time(),
                    'check_time' => 0
                ];
                $uid = $item['uid'];
                $qids[] = $key;
            }
            //todo 自动评论
            (new ZlcrCustomReportQuestionComment())->insertAll($inserData);
            //todo 修改问题状态为已评论
            $questionModel->where(['id' => ['in', $qids]])->update(['status' => 7]);
            $this->updateCache($uid, $qids);
        }
        catch (\Exception $e)
        {
            Log::write('获取超时未评论的问题数据失败：' . $e->getMessage(), 'error', true);
            return false;
        }
        return true;
    }

    /**
     * 更新问题缓存
     * @param $uid
     * @param $ids
     * @throws \Exception
     */
    private function updateCache($uid, $ids)
    {
        $questionModel = new ZlcrCustomReportQuestion();
        foreach ($ids as $id)
        {
            $questionModel->listDataDel($uid, [$id], 1);
            CacheManage::getInstance()->setCacheConfig($questionModel->cacheModel, $id)->rmCache();
        }
    }
}
